package com.local.controller;


import com.base.controller.BaseController;
import com.base.util.PageData;
import com.base.util.SessionUtil;
import com.base.util.Tools;
import com.frame.service.LoginManager;
import com.local.service.CommonManager;
import com.local.service.PersonalManager;
import com.local.service.ReportManager;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by lee on 2016/7/11.
 * 用户填报Controller
 */
@Controller
public class ReportController extends BaseController {

    @Resource(name = "reportService")
    private ReportManager reportManager;

    @Resource(name = "commonService")
    private CommonManager commonManager;

    @Resource(name = "loginService")
    private LoginManager loginManager;

    /*
    判断是否首次填写个人婚姻状况，首次填报之后，此字段不允许修改
 */
    @RequestMapping(value = "/getHunYinSingleFlag",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getHunYinSingleFlag(){
        PageData pd = this.getPageData();
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        pd.put("C_USERID",Double.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR", Tools.date2StrYear(new Date()));
        int i = 0;
        i = commonManager.getInfoCommonCount(pd,"GerenHunyinMapper.getGerenHunyinCount");
        JSONObject getObj = new JSONObject();
        getObj.put("data",i==0?"true":"false");
        return getObj.toString();
    }

    /**
     * 此方法，用来记录当年第一次填报时，记录填报日期
     */
    @ResponseBody
    @RequestMapping(value = "/report",produces = "application/json;charset=UTF-8")
    public void report(){
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        PageData info = this.getPageData();
        info.put("C_USERID",user.get("C_USERID").toString());
        info.put("NAME",user.getString("C_USERNAME"));
        info.put("RPTDATE",info.getString("STARTDATE"));
        info.put("UPDATETIME",new Date());
        info.put("RPTYEAR",Tools.date2StrYear(new Date()));
        reportManager.insertReport(info);
    }

    /**
     * 此方法，填报项导航带参数，来实现业务跳转
     */
    @ResponseBody
    @RequestMapping(value="/{rout}",produces="application/json;charset=UTF-8")
    public ModelAndView gotoMenu(@PathVariable String rout){
        ModelAndView mv = this.getModelAndView();
        PageData user = (PageData) SessionUtil.getHttpSession().getAttribute("USER");
        PageData pd = this.getPageData();
        pd.put("C_USERID",Double.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR",Tools.date2StrYear(new Date()));
        List<PageData> list = commonManager.getInfoCommon(pd,Tools.flag2Mapper(rout.toUpperCase()+"_INFS"));
        mv.addObject("deptname", loginManager.getDepartment(null));
        mv.addObject("list",list);
        if(rout.equals("gupiao")||rout.equals("jijin")){
            double value = 0;
            for(PageData gp : list){
                try{
                    value += Double.valueOf(gp.get("VALUEINFO").toString());
                }catch(Exception e){
                    value += 0;
                }
            }
            mv.addObject("value",value);
        }
        mv.setViewName("local/info/"+rout);
        return mv;
    }

    /**
     * 此方法，导航到提交信息确认页面
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/gotoCommit",produces="application/json;charset=UTF-8")
    public ModelAndView gotoCommit(){
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        String id = user.get("C_USERID").toString();
        pd.put("C_USERID",id);
        pd.put("RPTYEAR", Tools.date2StrYear(new Date()));

        PageData hunyin = commonManager.getInfoCommonByID(pd,"GerenHunyinMapper.getGerenHunyinByID");
        PageData salary = commonManager.getInfoCommonByID(pd,"PersonalInfoMapper.getSalaryByID");
        PageData laowu = commonManager.getInfoCommonByID(pd,"LaowuMapper.getLaowuByID");

        String hunyinStr = "";
        String salaryStr = "";
        String laowuStr = "";
        if(null!=hunyin&&null != hunyin.get("FIRSTREPORT")){
            hunyinStr = hunyin.get("FIRSTREPORT").toString();
        }
        if(null!=salary&&null != salary.get("SUM")){
            salaryStr = salary.get("SUM").toString();
        }
        if(null!=laowu&&null != laowu.get("SUM")){
            laowuStr = laowu.get("SUM").toString();
        }

        List<PageData> chuguodoclist = commonManager.getInfoCommon(pd,"ChuguodocMapper.getChuguodoc");
        String chuguodoc = "共："+chuguodoclist.size()+"条出国证件。";

        for(int i=0;i<chuguodoclist.size();i++){
            chuguodoc += "具体为："+(i+1) +
                    ")证件类型： "+chuguodoclist.get(i).getString("CHUGUODOCTYPE")+
                    "；证件号码："+chuguodoclist.get(i).getString("CHUGUODOCNO")+
                    "；发证日期："+chuguodoclist.get(i).getString("CHUGUODOCDATE")+
                    "；有效期："+chuguodoclist.get(i).getString("EXPIRYDATE")+
                    "；保管机构："+chuguodoclist.get(i).getString("CUSTODIAN");
        }
        List<PageData> chuguolist = commonManager.getInfoCommon(pd,"ChuguoMapper.getChuguo");
        String chuguo = "共："+chuguolist.size()+"条出国记录。";
        for(int i=0;i<chuguolist.size();i++){
            chuguo += "具体为："+(i+1)+
                    ")所到国家(地区)："+chuguolist.get(i).getString("VISTCOUNTRY")+
                    "；出境事由："+chuguolist.get(i).getString("REASONS")+
                    "；开始时间："+chuguolist.get(i).getString("STARTDATE")+
                    "；截止时间："+chuguolist.get(i).getString("ENDDATE");
        }
        List<PageData> zinvhunyinlist = commonManager.getInfoCommon(pd,"HunyinMapper.getHunyin");
        String zinvhunyin = "共："+zinvhunyinlist.size()+"条子女婚姻记录。";
        for(int i=0;i<zinvhunyinlist.size();i++){
            zinvhunyin += "具体为："+(i+1) +
                    ")称谓："+zinvhunyinlist.get(i).getString("RELATION")+
                    "；子女姓名："+zinvhunyinlist.get(i).getString("NAME")+
                    "；子女配偶名称："+zinvhunyinlist.get(i).getString("SPOUSENAME")+
                    "；子女配偶国籍(地区)："+zinvhunyinlist.get(i).getString("SPOUSECOUNTRY")+
                    "；工作(学习)单位："+zinvhunyinlist.get(i).getString("SPOUSEUNIT")+
                    "；职务："+zinvhunyinlist.get(i).getString("SPOUSEDUTY")+
                    "；婚姻登记时间："+zinvhunyinlist.get(i).getString("REGISTRATIONDATE");
        }
        List<PageData> zerenlist = commonManager.getInfoCommon(pd,"ZerenMapper.getZeren");
        String zeren = "共："+zerenlist.size()+"条刑事责任。";
        for(int i=0;i<zerenlist.size();i++){
            zeren += "具体为："+(i+1) +
                    ")称谓："+zerenlist.get(i).getString("RELATION")+
                    "；姓名："+zerenlist.get(i).getString("NAME")+
                    "；被追究时间："+zerenlist.get(i).getString("ZJDATA")+
                    "；刑事责任原因："+zerenlist.get(i).getString("RESASON")+
                    "；处理阶段："+zerenlist.get(i).getString("STAGE")+
                    "；处理结果："+zerenlist.get(i).getString("RESULT");
        }
        List<PageData> yijulist = commonManager.getInfoCommon(pd,"YijuMapper.getYiju");
        String yiju = "共："+yijulist.size()+"条移居情况。";
        for(int i=0;i<yijulist.size();i++){
            yiju += "具体为："+(i+1)+
                    ")称谓："+yijulist.get(i).getString("RELATION")+
                    "；姓名"+yijulist.get(i).getString("NAME")+
                    "；移居国家(地区)"+yijulist.get(i).getString("COUNTRY")+
                    "；城市"+yijulist.get(i).getString("CITY")+
                    "；移居证件号码"+yijulist.get(i).getString("ABROADID")+
                    "；移居类型"+yijulist.get(i).getString("TYPE")+
                    "；移居时间"+yijulist.get(i).getString("TRANSPLANTDATE")+
                    "；备注"+yijulist.get(i).getString("REMARK")+
                    "；移居国家："+yijulist.get(i).getString("COUNTRY");
        }

        List<PageData> jiuyelist = commonManager.getInfoCommon(pd,"JiuyeMapper.getJiuye");
        String jiuye = "共："+jiuyelist.size()+"条从业情况。";
        for(int i=0;i<jiuyelist.size();i++){
            jiuye += "具体为："+(i+1)+
                    ")称谓："+jiuyelist.get(i).getString("RELATION")+
                    "；姓名："+jiuyelist.get(i).getString("NAME")+
                    "；共同生活："+jiuyelist.get(i).getString("ISTOGETHER")+
                    "；工作学习单位："+jiuyelist.get(i).getString("UNIT")+
                    "；现任职务："+jiuyelist.get(i).getString("TITLE")+
                    "；单位性质："+jiuyelist.get(i).getString("UNIT_PROPERTY")+
                    "；证件名称："+jiuyelist.get(i).getString("CREDENTIALSTYPE")+
                    "；证件号码："+jiuyelist.get(i).getString("CREDENTIALSNO");
        }

        List<PageData> houselist = commonManager.getInfoCommon(pd,"PersonalInfoMapper.getHouse");
        String house = "共："+houselist.size()+"条房产情况。";
        for (int i=0;i<houselist.size();i++){
            house += "具体为："+(i+1)+
                    ")称谓："+houselist.get(i).getString("RELATION")+
                    "；产权人："+houselist.get(i).getString("NAME")+
                    "；来源去向："+houselist.get(i).getString("HOUSELYQX")+
                    "；具体地址："+houselist.get(i).getString("ADDRESS")+
                    "；建筑面积："+houselist.get(i).getString("AREA")+
                    "；产权性质："+houselist.get(i).getString("PROPERTY")+
                    "；交易价格："+houselist.get(i).getString("VALUE");
        }

        List<PageData> gupiaolist = commonManager.getInfoCommon(pd,"PersonalInfoMapper.getGupiao");
        String gupiao = "共："+gupiaolist.size()+"只股票。";
        for(int i=0;i<gupiaolist.size();i++){
            gupiao += "具体为："+(i+1)+")称谓："+gupiaolist.get(i).getString("RELATION")+"；股票名称："+gupiaolist.get(i).getString("PRODUCTS")+"；前一交易日净值："+gupiaolist.get(i).getString("VALUEINFO")+"。 ";
        }
        List<PageData> jijinlist = commonManager.getInfoCommon(pd,"PersonalInfoMapper.getJijin");
        String jijin = "共："+jijinlist.size()+"只基金。";
        for(int i=0;i<jijinlist.size();i++){
            jijin += "具体为："+(i+1)+")称谓："+jijinlist.get(i).getString("RELATION")+"；股票名称："+jijinlist.get(i).getString("PRODUCTS")+"；前一交易日净值："+jijinlist.get(i).getString("VALUEINFO")+"。 ";

        }
        List<PageData> qihuolist = commonManager.getInfoCommon(pd,"PersonalInfoMapper.getQihuo");
        String qihuo = "共："+jijinlist.size()+"只期货。";
        for(int i=0;i<qihuolist.size();i++){
            qihuo += "具体为："+(i+1)+")称谓："+qihuolist.get(i).getString("RELATION")+"；期货名称："+qihuolist.get(i).getString("PRODUCTS")+"。 ";
        }

        List<PageData> qiyelist = commonManager.getInfoCommon(pd,"PersonalInfoMapper.getQiye");
        String qiye = "共："+qiyelist.size()+"条非上市企业信息。";
        for(int i=0;i<qiyelist.size();i++){
            qiye += "具体为："+(i+1)+
                    ")称谓："+qiyelist.get(i).getString("RELATION")+
                    "；姓名："+qiyelist.get(i).getString("NAME")+
                    "；经营范围："+qiyelist.get(i).getString("FANWEI")+
                    "；注册资本(金)(万元)："+qiyelist.get(i).getString("ZHUCEZIBEN")+
                    "；认缴出资额(万元)："+qiyelist.get(i).getString("CHUZIER")+
                    "；出资比例(万元)："+qiyelist.get(i).getString("CHUZIBILI")+
                    "；企业名称："+qiyelist.get(i).getString("QIYENAME");
        }

        List<PageData> getilist = commonManager.getInfoCommon(pd,"PersonalInfoMapper.getGeti");
        String geti = "共："+getilist.size()+"条个体企业信息。";
        for(int i=0;i<getilist.size();i++){
            geti += "具体为："+(i+1)+
                    ")称谓："+getilist.get(i).getString("RELATION")+
                    "；姓名："+getilist.get(i).getString("NAME")+
                    "；市场主体名称："+getilist.get(i).getString("QIYENAME")+
                    "；经营范围(业务范围)："+getilist.get(i).getString("FANWEI")+
                    "；市场主体类型："+getilist.get(i).getString("TYPE")+
                    "；资金数额(出资额)(万元)："+getilist.get(i).getString("ZHUCEZIBEN")+
                    "；个人出资额(万元)："+getilist.get(i).getString("CHUZIER")+
                    "；出资比例(万元)："+getilist.get(i).getString("CHUZIBILI");
        }
        mv.addObject("hunyin",hunyinStr);
        mv.addObject("salary",salaryStr);
        mv.addObject("laowu",laowuStr);
        mv.addObject("chuguodoc",chuguodoc);
        mv.addObject("chuguo",chuguo);
        mv.addObject("geti",geti);
        mv.addObject("qiye",qiye);
        mv.addObject("qihuo",qihuo);
        mv.addObject("jijin",jijin);
        mv.addObject("gupiao",gupiao);
        mv.addObject("house",house);
        mv.addObject("jiuye",jiuye);
        mv.addObject("yiju",yiju);
        mv.addObject("zeren",zeren);
        mv.addObject("zinvhunyin",zinvhunyin);
        mv.setViewName("local/info/commit");
        return mv;
    }

    /**
     * 此方法，提交报批信息
     */
    @ResponseBody
    @RequestMapping(value="/commit",produces="application/json;charset=UTF-8")
    public void commit(){
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        PageData pd = this.getPageData();
        pd.put("UPDATETIME",Tools.str2Date(Tools.date2Str(new Date())));
        pd.put("C_USERID",Integer.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR",Tools.date2StrYear(new Date()));
        pd.put("UIFLAG","UPD");
        commonManager.insertUpdCommon(pd,"ReportMapper.updReportInfoByCommit");
    }


}
